package com.park.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.park.dto.ParkingSlot;
import com.park.entity.Park;
import com.park.service.ParkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ParkingController {
    private static final int SLOT_CAPACITY = 100;
    private static final List<ParkingSlot> parkingSlots = new ArrayList<>(SLOT_CAPACITY);
    private static final Logger logger = LoggerFactory.getLogger(ParkingController.class);

    static {
        for (int i=0;i<SLOT_CAPACITY;i++) {
            parkingSlots.add(new ParkingSlot(i + 1, true, null));
        }
    }

    @Autowired
    private ParkService ps;

    @GetMapping("/parking/search")
    @ResponseBody
    public List searchByCarId(@RequestParam(value = "carId", required = false) String carId, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize) {
        List<Object> ret = new ArrayList<>();
        Page<Park> parkPage = new Page<>();
        parkPage.setSize(pageSize);
        parkPage.setCurrent(page + 1);
        updateSlot(); //测试停车位信息搜索功能的，正式上线后应该删除此行
        if (carId == null) {
            List<Park> res = ps.list();
            ret.addAll(res.subList(page * pageSize, Math.min((page + 1) * pageSize, res.size())));
            ret.add(res.size() % pageSize == 0 ? res.size() / pageSize : res.size() / pageSize + 1);
            return ret;
        }
        ret.addAll(ps.getByParkCarId(parkPage, carId).getRecords());
        ret.add(parkPage.getTotal() % pageSize == 0 ? parkPage.getTotal() / pageSize : parkPage.getTotal() / pageSize + 1);
        return ret;
    }

    @GetMapping("/parking.html")
    public String index() {
        return "parking";
    }

    @GetMapping("/park_info.html")
    public String info() {
        return "park_info";
    }

    @GetMapping("/park_info/search")
    @ResponseBody
    public List<Object> searchParkingSlot(@RequestParam(value = "slotStatus", required = false)String status, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize) {
        List<Object> ret = new ArrayList<>();
        if (status == null) status = "";
        switch (status) {
            case "idle":
                List<ParkingSlot> tmp = new ArrayList<>();
                parkingSlots.forEach(e -> {
                    if (e.isIdle()) tmp.add(e.setParkCarId(null));
                });
                ret.addAll(tmp.subList(page * pageSize, Math.min((page + 1) * pageSize, tmp.size())));
                ret.add(tmp.size() % pageSize == 0 ? tmp.size() / pageSize : tmp.size() / pageSize + 1);
                break;
            case "using":
                tmp = new ArrayList<>();
                parkingSlots.forEach(e -> {
                    if (!e.isIdle()) tmp.add(e);
                });
                ret.addAll(tmp.subList(page * pageSize, Math.min((page + 1) * pageSize, tmp.size())));
                ret.add(tmp.size() % pageSize == 0 ? tmp.size() / pageSize : tmp.size() / pageSize + 1);
                break;
            case "all":
            default:
                ret.addAll(parkingSlots.subList(page * pageSize, Math.min((page + 1) * pageSize, parkingSlots.size())));
                ret.add(SLOT_CAPACITY % pageSize == 0 ? SLOT_CAPACITY / pageSize : SLOT_CAPACITY / pageSize + 1);
                break;
        }
        return ret;
    }

    @GetMapping("/park_info/update")
    @ResponseBody
    public String updateSlot() {
        logger.info("Updating parking slot info...");
        parkingSlots.forEach(e -> e.setIdle(true));
        List<Park> parkList = ps.getByParkOutTimeIsNull();
        parkList.forEach(e -> {
            parkingSlots.get(e.getParkPosition()).setIdle(false);
            parkingSlots.get(e.getParkPosition()).setParkCarId(e.getParkCarId());
        });
        logger.info("Parking slot info was up-to-date.");
        return "{\"msg\" : \"success\"}";
    }
}
